# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json

requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
中国商标网
http://wsgg.sbj.cnipa.gov.cn:9080/tmann/annInfoView/annSearch.html
"""



class ShangBiao():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            "User-Agent": ua.random,
            "Accept": "application/json, text/javascript, */*; q=0.01",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
            "DNT": "1",
            "Host": "wsgg.sbj.cnipa.gov.cn:9080",
            "Origin": "http://wsgg.sbj.cnipa.gov.cn:9080",
            "Referer": "http://wsgg.sbj.cnipa.gov.cn:9080/tmann/annInfoView/annSearch.html?annNum=&tdsourcetag=s_pctim_aiomsg",
            "X-Requested-With": "XMLHttpRequest",
            "COOKIE": "__jsluid_h=bcfe6f483314af9c0bc07682ee763b63; Hm_lvt_d7682ab43891c68a00de46e9ce5b76aa=1583380659; Hm_lpvt_d7682ab43891c68a00de46e9ce5b76aa=1583380659; tmas_cookie=51947.7683.15402.0000; UM_distinctid=170a8d822477c0-0122732caf27cc-4313f6b-144000-170a8d822485ac; JSESSIONID=00005JtoNCQjD2SLbQl1XNFp7_Y:1bm112s99"
        }

    def get_contents(self):
        postData = {
            "page": "1",
            "rows": 20,
            "annNum": "",
            "annType": "",
            "tmType": "",
            "coowner": "",
            "recUserName": "",
            "allowUserName": "",
            "byAllowUserName": "",
            "appId": "",
            "appIdZhiquan": "",
            "bfchangedAgengedName": "",
            "changeLastName": "",
            "transferUserName": "",
            "acceptUserName": "",
            "regName": "华为技术有限公司",
            "tmName": "",
            "intCls": "",
            "fileType": "",
            "totalYOrN": "false",
            "appDateBegin": "",
            "appDateEnd": "",
            "agentName": "",
        }
        url = "http://wsgg.sbj.cnipa.gov.cn:9080/tmann/annInfoView/annSearchDG.html"
        html = HttpUtils.do_request("POST", url,self.headers, postData)
        datajson = json.loads(html.text)
        for d in datajson['rows']:
            dict_data = dict()
            dict_data['ann_num'] = d['ann_num']  # 公告号
            dict_data['ann_date'] = d['ann_date']  # 公告日期
            dict_data['ann_type'] = d['ann_type']  # 公告类型
            dict_data['reg_num'] = d['reg_num']  # 注册号
            dict_data['tm_name'] = d['tm_name']  # 商标名称
            print(str(dict_data))

            self.get_gonggao(d['ann_num'])



    def get_gonggao(self, ann_num):
        postData = {
            "annNum": ann_num,
            "page": "1",
            "rows": 20,
        }
        url = "http://wsgg.sbj.cnipa.gov.cn:9080/tmann/annInfoView/getCatalogueInfo.html"
        html = HttpUtils.do_request("POST", url, self.headers, postData)
        datajson = json.loads(html.text)
        for d in datajson['rows']:
            dict_data = dict()
            dict_data['case_num'] = d['case_num']  # 序号
            dict_data['ann_num'] = d['ann_num']  # 公告期号
            dict_data['ann_date'] = d['ann_date']  # 公告日期


if __name__ == '__main__':
    shangbiao = ShangBiao()
    shangbiao.get_contents()